package wrapper

import (
	"gitee.com/ymofen/gobase"
	"strings"
	"time"
)

var (
	EmptySVObject *SuperValue = NewSVObject()
	zeroTime      time.Time
)

func SV2StrMap(strmap gobase.StrMap, sv *SuperValue) {
	if sv == nil {
		return
	}
	if sv.IsString() {
		strmap.URLFormDecode(sv.String())
	} else if sv.IsObject() {
		sv.Range(func(key string, val *SuperValue) bool {
			strmap[key] = val.String()
			return true
		})
	}
}

func isJsonObjectString(s string) bool {
	l := len(s)
	return s[0] == '{' && s[l-1] == '}'
}

func isJsonArrayString(s string) bool {
	l := len(s)
	return s[0] == '[' && s[l-1] == ']'
}

func IsJsonString(s string) bool {
	l := len(s)
	return l >= 2 && (isJsonObjectString(s) || isJsonArrayString(s))
}

/*
*

	去掉注释
*/
func TryFixJSONString(s string) string {

	// s = gobase.ReplaceArea(s, "//", "\n", "\n")
	s, _ = gobase.ReplaceReg(s, "\\s\\/\\/.*", "\n")
	s = gobase.ReplaceArea(s, "/*", "*/", "")
	return s
}

func CacheListNum() int32 {
	return 0
	//return dxsvalue.ValueCacheNum
}

func (this *SuperValue) ChangeValueAsDateTimeString(key string) {
	val := this.AsDateTime(key, zeroTime)
	this.SetKeyValue(key, gobase.DateTimeString2(val))
}

func (this *SuperValue) CheckSetDefaultVal(key string, def interface{}) {
	val := this.val.ValueByName(key)
	if val == nil {
		this.SetKeyValue(key, def)
	}
}

/*
**

	判断字段中是否有一个字段值为空
	fields用;分割
*/
func (this *SuperValue) IfOneOfFieldsIsEmpty(fields string) bool {
	j := 0 // 为空的字段数

	fieldn := len(strings.Split(fields, ";")) // 传入的字段数量

	// n为循环了多少个字段
	n := this.RangeFieldsStrV(fields, func(key string, val string) bool {
		if len(val) == 0 { // 有一个为空, 退出循环
			j++
			return false
		}
		return true
	})

	// 还有字段没有传入值
	return j > 0 || n < fieldn
}

/*
**

	fields: "fld1;fld2=srcfld2;
*/
func (this *SuperValue) CopyFromByFields(src *SuperValue, fields string) int {
	j := 0
	fds := strings.Split(fields, ";")
	for i := 0; i < len(fds); i++ {
		fd := fds[i]
		if len(fd) > 0 {
			fd1, fd2 := gobase.Split2Str(fd, "=")
			fd1 = gobase.Trim(fd1)
			if len(fd1) == 0 {
				continue
			}
			if len(fd2) == 0 {
				fd2 = fd1
			}
			srcv := src.val.ValueByName(fd2)
			if srcv != nil {
				this.val.SetKeyValue(fd1, srcv)
				j++
			}
		}
	}
	return j
}

/*
**

	fields: "fkey1=fnewkey;
*/
func (this *SuperValue) RenameByFields(fields string) int {
	j := 0
	fds := strings.Split(fields, ";")
	for i := 0; i < len(fds); i++ {
		fd := fds[i]
		if len(fd) > 0 {
			fd1, fd2 := gobase.Split2Str(fd, "=")
			fd1 = gobase.Trim(fd1)
			if len(fd1) == 0 {
				continue
			}

			srcv := this.val.ValueByName(fd1)
			if srcv != nil {
				this.val.SetKeyValue(fd2, srcv)
				this.val.RemoveKey(fd1)
				j++
			}
		}
	}
	return j
}

/*
**

	判断字段中是否有一个字段值为空
	fields用;分割
*/
func (this *SuperValue) RemoveFields(fields string) int {

	if len(fields) == 0 {
		return 0
	}
	if this.val == nil {
		return 0
	}

	n := 0
	gobase.RangeStringSep2(fields, func(idx int, s1 string) bool {
		if len(s1) > 0 {
			this.val.RemoveKey(s1)
			n++
		}
		return true
	}, ',', ';')
	return n
}

func (this *SuperValue) FindByChild(childPath string, matchval interface{}) (rval *SuperValue) {
	paths := strings.Split(childPath, ".")
	this.Range(func(key string, val *SuperValue) bool {
		v1 := val.val.ValueByPath(paths...)
		if dxvalueEqV(v1, matchval) {
			rval = val
		}
		return rval == nil
	})
	return
}
